using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace Needle.Core.Configuration {
    /// <summary>
    /// The collection of injected arguments
    /// </summary>
    public abstract class NeedleElementCollection : ConfigurationElementCollection, ICollection<ConfigurationElement> {
        
        /// <summary>
        /// The collection type.
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType {
            get {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        /// <summary>
        /// Create a new element.
        /// </summary>
        protected abstract override ConfigurationElement CreateNewElement();

        /// <summary>
        /// gets the key of the  element (the index).
        /// </summary>
        protected abstract override object GetElementKey(ConfigurationElement element);

        /// <summary>
        /// The name of the item xml element.
        /// </summary>
        protected abstract override string ElementName  {  get;  }

        #region ICollection<ConfigurationElement> Members

        /// <summary>
        /// Adds an element to the collection 
        /// </summary>
        /// <param name="item"></param>
        public void Add(ConfigurationElement item)
        {
            object key = GetElementKey(item); /* type check */
            BaseAdd(item);
        }
        /// <summary>
        /// Clears the collectionType
        /// </summary>
        public void Clear()
        {
            BaseClear();
        }
        /// <summary>
        /// returns true if the collection contains the element.
        /// </summary>
         public bool Contains(ConfigurationElement item)
        {
            return BaseIndexOf(item) > -1;
        }

         /// <summary>
         /// returns true if the collection is read only.
         /// </summary>
         public new bool IsReadOnly
        {
            get { return LockItem; }
        }
        /// <summary>
        /// Removes the item from the collection, 
        /// returns true if the collection was modified.
        /// </summary>
        public bool Remove(ConfigurationElement item)
        {
            int index = BaseIndexOf(item);
            bool found = index > -1;
            if (found) BaseRemoveAt(index);
            return found;
        }

        #endregion

        #region IEnumerable<ConfigurationElement> Members
        /// <summary>
        /// Returns an enumerator for the elements in this collection.
        /// </summary>
        public new IEnumerator<ConfigurationElement> GetEnumerator()
        {
            IEnumerator e = base.GetEnumerator();
            while (e.MoveNext())
            {
                yield return (ConfigurationElement)e.Current;
            }
            yield break;
        }

        #endregion
    }
}
